import dayjs, { extend } from 'dayjs'
import duration from 'dayjs/plugin/duration'
import utc from 'dayjs/plugin/utc'

extend(utc)
extend(duration)
export function getDate(date: string, type: 'Y' | 'M' | 'D' = 'Y') {
  const obj = {
    Y: '$1',
    M: '$2',
    D: '$3',
  }
  return date.replace(/^(\d{4})-(\d{2})-(\d{2})$/, obj[type])
}

export function utc2BJ(date: any, fmt = 'YYYY-MM-DD HH:mm', tz = 8) {
  return dayjs(date).add(tz, 'hour').format(fmt)
}
type DateType = 'hour' | 'month'
export function someDate({
  date,
  fmt = 'YYYY-MM-DD HH:mm',
  num = 8,
  type = 'hour',
}: { date: any, fmt?: string, num?: number, type?: DateType }) {
  if (!date)
    return ''
  return dayjs(date).add(num, type).format(fmt)
}
